package com.example.passport.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Dashboard;
import com.example.passport.entity.Role;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO `user`" +
            " (`tenant_id`,`mobile`,`username`,`name`,`password_hash`,`password_salt`,`api_token`,`superior_id`,`event_id`,`to_user`,`dep_id`) VALUES" +
            " (#{tenantId}, #{mobile}, #{username}, #{name}, #{passwordHash}, #{passwordSalt}, #{apiToken},#{superiorId},#{eventId},#{toUser},#{depId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int create(User user);
//
    @Update("UPDATE `user` SET `status` = #{status}" +
            " WHERE `id`=#{userId} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int updateStatus(@Param("userId") long userId, @Param("tenantId") long tenantId, @Param("status") int status);

    @Update("UPDATE `user` SET `username`=#{username},`name`=#{name},mobile=#{mobile},email=#{email}" +
            " WHERE `id`=#{id}")
    int updateUser(User user);

    @Update("UPDATE `user` SET `password_hash` = #{passwordHash}" +
            " WHERE `id`=#{userId} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int updatePassword(@Param("userId") long userId, @Param("tenantId") long tenantId, @Param("passwordHash") String passwordHash);
//
    @Select("<script>" +
            "SELECT * FROM `user`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.username!=null'> AND `username` LIKE CONCAT('%',#{condition.username},'%') </if>" +
            "<if test='condition.status!=0'> AND `status`=#{condition.status} </if>" +
            "<if test='condition.depId!=null and condition.depId!=0'> AND `dep_id`=#{condition.depId} </if>" +
            " AND `tenant_id`=#{condition.tenantId} AND `status`!=" + User.STATUS_DELETE +
            "</where>" +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<User> findAll(@Param("condition") User condition, @Param("pagination") Pagination pagination);
//
    @Select("<script>" +
            "SELECT COUNT(*) FROM `user`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.depId!=null and condition.depId!=0'> AND `dep_id`=#{condition.depId}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.username!=null'> AND `username` LIKE CONCAT('%',#{condition.username},'%') </if>" +
            "<if test='condition.status!=0'> AND `status`=#{condition.status} </if>" +
            "AND `tenant_id`=#{condition.tenantId}  AND `status`!=" + User.STATUS_DELETE +
            "</where>" +
            "</script>")
    long count(@Param("condition") User condition);

    @Select("SELECT * FROM user as u,user_role as ur WHERE u.id=#{id} AND ur.user_id = u.id  AND u.status != " + User.STATUS_DELETE + " LIMIT 1")
    User findById(@Param("id") long id );

    @Select("SELECT * FROM user WHERE id=#{id} AND status != " + User.STATUS_DELETE + " LIMIT 1")
    User findByIdTd(@Param("id") Long id);

    @Select("SELECT * FROM user WHERE id=#{id} AND `tenant_id`=#{tenantId} LIMIT 1")
    User findByIdAndDel(@Param("id") Long id ,@Param("tenantId") Long tenantId);

    @Select("SELECT * FROM user WHERE mobile = #{mobile} AND status != " + User.STATUS_DELETE + " LIMIT 1")
    User findByMobile(@Param("mobile") String mobile);

    @Select("SELECT * FROM user WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `mobile` = #{mobile}  AND `status`!=" + User.STATUS_DELETE+ " LIMIT 1")
    User findByIdMobile(@Param("tenantId") long tenantId, @Param("id") long id , @Param("mobile") String mobile);

    @Select("SELECT * FROM user WHERE `username` = #{username} AND `tenant_id`=#{tenantId} AND status != " + User.STATUS_DELETE + " LIMIT 1")
    User findByUserName(@Param("username") String username,@Param("tenantId") Long tenantId);

    @Select("SELECT * FROM user WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `username` = #{username} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    User findByIdUserName(@Param("tenantId") long tenantId, @Param("id") long id , @Param("username") String username);

    @Update("UPDATE `user` SET " +
            "`name` = #{user.name} " +
            ", `username` = #{user.username}" +
            ", `mobile` = #{user.mobile}" +
            ", `event_id` = #{user.eventId}" +
            ", `superior_id` = #{user.superiorId}" +
            ", `to_user` = #{user.toUser}" +
            ", `dep_id` = #{user.depId}" +
            " WHERE `id`=#{user.id} AND `tenant_id`=#{user.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int update(@Param("user") User user);

    @Select("SELECT * FROM user WHERE `username` = #{username} AND status != " + User.STATUS_DELETE + " LIMIT 1")
    User findByUsername(@Param("username") String username);

    @Update("UPDATE `user` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(@Param("id") long id,@Param("tenantId") long tenantId);


    @Select("<script>" +
            "SELECT * FROM `user` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='dashboardIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<User> findByIds(@Param("dashboardIds") List<String> dashboardIds);

    @Select("SELECT * from `user` where dep_id = #{depId} AND `status`!=" + User.STATUS_DELETE)
    List<User> findByIdDepId(Long depId);

}
